package cg.studio.distribute.vote.server;

import cg.studio.distribute.vote.client.EndUser;
import cg.studio.distribute.vote.site.Site;

/**
 * Assign site to user
 * Strategy: each site main same users
 * 
 * @author Bright Chen
 *
 */
public class EvenSiteAssigner implements SiteAssigner
{
  private Site[] sites;
  private volatile int index = -1;
  
  public EvenSiteAssigner( int siteNum )
  {
    sites = new Site[ siteNum ];
  }
  
  /**
   * try to avoid lock
   */
  public Site getOwnerSite( EndUser user )
  {
    int thisIndex = ++index;
    
    thisIndex %= sites.length;
    return sites[ thisIndex ];
  }
  
  public Site[] getSites()
  {
    return sites.clone();
  }
}
